capture log close
clear all
set maxvar 10000
set more off
pause off
# delimit;

/* 
Figure VI 
*/

local data "" ;
local output "" ;

foreach gp in all singno male female nonment othmen intel phys nonphys twopar parhigh parlow white black ueallhigh uealllow { ;
	use "prwora_bw264_crime_poly1allcov_iv_`gp'.dta", clear ;
	keep if 
		(
		variables == "n_crime2genr_1997_2017"	| 
		variables == "n_crime2ahinc_1997_2017" | 
		variables == "avg_kidemp15_1997_2017")
		& sample == "age18unfav" ;
	foreach stub in 
		genr ahinc { ;
			replace variables = "age18rd_`stub'"   if variables == "n_crime2`stub'_1997_2017" ;
	} ;
		replace variables = "age18rd_kidemp15" if variables == "avg_kidemp15_1997_2017" ;
	rename n_age18unfav b_endo ;
	rename sen_age18unfav se_endo ;
	keep variables b_endo se_endo mean_rd ;
	gen gp = "`gp'" ;
	gen exp = "age18rd" ;
	tempfile `gp' ;
	save ``gp'' ;
} ;

/* Family structure */
local fam1 "twopar" ;
local fam2 "singno" ;
local fam1_label "Two parents" ;
local fam2_label "Single/no parents" ;
local fam1_color "purple" ;
local fam2_color "orange" ;

/* Parent earnings */
local earn1 "parhigh" ;
local earn2 "parlow" ;
local earn1_label "High parent earn" ;
local earn2_label "Low parent earn" ;
local earn1_color "purple" ;
local earn2_color "orange" ;

/* Race */
local race1 "white" ;
local race2 "black" ;
local race1_label "White" ;
local race2_label "Black" ;
local race1_color "teal" ;
local race2_color "olive" ;

/* Sex */
local sex1 "female" ;
local sex2 "male" ;
local sex1_label "Female" ;
local sex2_label "Male" ;
local sex1_color "pink" ;
local sex2_color "blue" ;

/* Diagnosis */
local diag1 "nonment" ;
local diag2 "othmen" ;
local diag3 "phys" ;
local diag4 "intel" ;
local diag5 "nonphys" ;
local diag1_label "Physical or ID" ;
local diag2_label "Other mental" ;
local diag3_label "Physical" ;
local diag4_label "Intellectual" ;
local diag5_label "All mental" ;
local diag1_color "green" ;
local diag2_color "orange" ;
local diag3_color "green" ;
local diag4_color "teal" ;

/* Unemployment rate */
local ue1 "uealllow" ;
local ue2 "ueallhigh" ;

local ysc_genrt "0(.5)2" ;
local ysc_ainca "0(.05).2" ;
local ysc_ahinc "0(.05).2" ;

local label_genrt "income-generating charge" ;
local label_ainca "incarceration" ;
local label_ahinc "incarceration" ;


use `all', clear ;		 
append using ``fam1'' ;
append using ``fam2'' ;
append using ``earn1'' ;
append using ``earn2'' ;
append using ``sex1'' ;
append using ``sex2'' ;
append using ``diag2'' ;
append using ``diag3'' ;
append using ``diag4'' ;
append using ``diag5'' ;
append using ``race1'' ;
append using ``race2'' ;
append using ``ue1'' ;
append using ``ue2'' ;

gen treat_lvl = mean_rd + b_endo ;

gen lo95 = treat_lvl - 1.96*se_endo ;
gen hi95 = treat_lvl + 1.96*se_endo ;


/* Main heterogeneity graph */
capture drop sort ;

local add_genr     = 0 ;
local add_kidemp15 = .15 ;
local add_ahinc    = -.15 ;
local add_ainca    = -.15 ;

gen sort = . ;

foreach crime in genr ahinc ainca kidemp15 { ;

	replace	sort = 24 + `add_`crime''  if variables == "age18rd_`crime'" 	& gp == "all" ;
	replace	sort = 22 + `add_`crime''  if variables == "age18rd_`crime'" 	& gp == "`fam1'" ;
	replace	sort = 20 + `add_`crime''  if variables == "age18rd_`crime'" 	& gp == "`fam2'" ;
	replace	sort = 18 + `add_`crime''  if variables == "age18rd_`crime'" 	& gp == "`earn1'" ;
	replace	sort = 16 + `add_`crime''  if variables == "age18rd_`crime'" 	& gp == "`earn2'" ;
	replace	sort = 14 + `add_`crime''  if variables == "age18rd_`crime'" 	& gp == "`race1'" ;
	replace	sort = 12 + `add_`crime''  if variables == "age18rd_`crime'" 	& gp == "`race2'" ;
	replace	sort = 10 + `add_`crime'' if variables == "age18rd_`crime'" 	& gp == "`sex1'" ;
	replace	sort = 8 + `add_`crime'' if variables == "age18rd_`crime'" 		& gp == "`sex2'" ;
	replace	sort = 6 + `add_`crime'' if variables == "age18rd_`crime'" 		& gp == "`diag4'" ;
	replace	sort = 4 + `add_`crime'' if variables == "age18rd_`crime'" 		& gp == "`diag3'" ;
	replace	sort = 2 + `add_`crime'' if variables == "age18rd_`crime'" 		& gp == "`diag2'" ;
	replace	sort = 0 + `add_`crime'' if variables == "age18rd_`crime'" 		& gp == "`ue1'" ;
	replace	sort = -2 + `add_`crime'' if variables == "age18rd_`crime'" 	& gp == "`ue2'" ;
} ;


twoway 

		(pcarrow sort mean_rd sort treat_lvl   	if variables == "age18rd_genr" & gp == "all", color(orange))
		(pcarrow sort mean_rd sort treat_lvl   	if variables == "age18rd_genr" & gp == "`fam1'", color(orange))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_genr" & gp == "`fam2'", color(orange))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_genr" & gp == "`earn1'", color(orange))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_genr" & gp == "`earn2'", color(orange))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_genr" & gp == "`sex1'", color(orange))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_genr" & gp == "`sex2'", color(orange))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_genr" & gp == "`diag2'", color(orange))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_genr" & gp == "`diag3'", color(orange))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_genr" & gp == "`diag4'", color(orange))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_genr" & gp == "`race1'", color(orange))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_genr" & gp == "`race2'", color(orange))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_genr" & gp == "`ue1'", color(orange))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_genr" & gp == "`ue2'", color(orange))
		
		(pcarrow sort mean_rd sort treat_lvl   	if variables == "age18rd_ahinc" & gp == "all", color(red*1.5) msize(vsmall))
		(pcarrow sort mean_rd sort treat_lvl   	if variables == "age18rd_ahinc" & gp == "`fam1'", color(red*1.5) msize(vsmall))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_ahinc" & gp == "`fam2'", color(red*1.5) msize(vsmall))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_ahinc" & gp == "`earn1'", color(red*1.5) msize(vsmall))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_ahinc" & gp == "`earn2'", color(red*1.5) msize(vsmall))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_ahinc" & gp == "`sex1'", color(red*1.5) msize(vsmall))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_ahinc" & gp == "`sex2'", color(red*1.5) msize(vsmall))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_ahinc" & gp == "`diag2'", color(red*1.5) msize(vsmall))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_ahinc" & gp == "`diag3'", color(red*1.5) msize(vsmall))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_ahinc" & gp == "`diag4'", color(red*1.5) msize(vsmall))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_ahinc" & gp == "`race1'", color(red*1.5) msize(vsmall))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_ahinc" & gp == "`race2'", color(red*1.5) msize(vsmall))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_ahinc" & gp == "`ue1'", color(red*1.5) msize(vsmall))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_ahinc" & gp == "`ue2'", color(red*1.5) msize(vsmall))
		
		(pcarrow sort mean_rd sort treat_lvl   	if variables == "age18rd_kidemp15" & gp == "all", color(green) msize(small))
		(pcarrow sort mean_rd sort treat_lvl   	if variables == "age18rd_kidemp15" & gp == "`fam1'", color(green) msize(small))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_kidemp15" & gp == "`fam2'", color(green) msize(small))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_kidemp15" & gp == "`earn1'", color(green) msize(small))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_kidemp15" & gp == "`earn2'", color(green) msize(small))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_kidemp15" & gp == "`sex1'", color(green) msize(small))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_kidemp15" & gp == "`sex2'", color(green) msize(small))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_kidemp15" & gp == "`diag2'", color(green) msize(small))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_kidemp15" & gp == "`diag3'", color(green) msize(small))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_kidemp15" & gp == "`diag4'", color(green) msize(small))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_kidemp15" & gp == "`race1'", color(green) msize(small))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_kidemp15" & gp == "`race2'", color(green) msize(small))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_kidemp15" & gp == "`ue1'", color(green) msize(small))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_kidemp15" & gp == "`ue2'", color(green) msize(small))
		
		(scatter sort mean_rd  if variables == "age18rd_genr" & gp == "all", color(orange) msymbol(X))
		(scatter sort mean_rd  if variables == "age18rd_genr" & gp == "`fam1'", color(orange) msymbol(X))
		(scatter sort mean_rd  if variables == "age18rd_genr" & gp == "`fam2'", color(orange) msymbol(X))
		(scatter sort mean_rd 	if variables == "age18rd_genr" & gp == "`earn1'", color(orange) msymbol(X))
		(scatter sort mean_rd 	if variables == "age18rd_genr" & gp == "`earn2'", color(orange) msymbol(X))
		(scatter sort mean_rd 	if variables == "age18rd_genr" & gp == "`sex1'", color(orange) msymbol(X))
		(scatter sort mean_rd	if variables == "age18rd_genr" & gp == "`sex2'", color(orange) msymbol(X))
		(scatter sort mean_rd 	if variables == "age18rd_genr" & gp == "`diag2'", color(orange) msymbol(X))
		(scatter sort mean_rd 	if variables == "age18rd_genr" & gp == "`diag3'", color(orange) msymbol(X))
		(scatter sort mean_rd 	if variables == "age18rd_genr" & gp == "`diag4'", color(orange) msymbol(X))
		(scatter sort mean_rd 	if variables == "age18rd_genr" & gp == "`race1'", color(orange) msymbol(X))
		(scatter sort mean_rd 	if variables == "age18rd_genr" & gp == "`race2'", color(orange) msymbol(X))
		(scatter sort mean_rd 	if variables == "age18rd_genr" & gp == "`ue1'", color(orange) msymbol(X))
		(scatter sort mean_rd 	if variables == "age18rd_genr" & gp == "`ue2'", color(orange) msymbol(X))
		
		(scatter sort mean_rd   if variables == "age18rd_ahinc" & gp == "all", color(red*1.5) msize(small) msymbol(X))
		(scatter sort mean_rd   if variables == "age18rd_ahinc" & gp == "`fam1'", color(red*1.5) msize(small) msymbol(X))
		(scatter sort mean_rd 	if variables == "age18rd_ahinc" & gp == "`fam2'", color(red*1.5) msize(small) msymbol(X))
		(scatter sort mean_rd 	if variables == "age18rd_ahinc" & gp == "`earn1'", color(red*1.5) msize(small) msymbol(X))
		(scatter sort mean_rd 	if variables == "age18rd_ahinc" & gp == "`earn2'", color(red*1.5) msize(small) msymbol(X))
		(scatter sort mean_rd 	if variables == "age18rd_ahinc" & gp == "`sex1'", color(red*1.5) msize(small) msymbol(X))
		(scatter sort mean_rd 	if variables == "age18rd_ahinc" & gp == "`sex2'", color(red*1.5) msize(small) msymbol(X))
		(scatter sort mean_rd 	if variables == "age18rd_ahinc" & gp == "`diag2'", color(red*1.5) msize(small) msymbol(X))
		(scatter sort mean_rd 	if variables == "age18rd_ahinc" & gp == "`diag3'", color(red*1.5) msize(small) msymbol(X))
		(scatter sort mean_rd 	if variables == "age18rd_ahinc" & gp == "`diag4'", color(red*1.5) msize(small) msymbol(X))
		(scatter sort mean_rd 	if variables == "age18rd_ahinc" & gp == "`race1'", color(red*1.5) msize(small) msymbol(X))
		(scatter sort mean_rd 	if variables == "age18rd_ahinc" & gp == "`race2'", color(red*1.5) msize(small) msymbol(X))
		(scatter sort mean_rd 	if variables == "age18rd_ahinc" & gp == "`ue1'", color(red*1.5) msize(small) msymbol(X))
		(scatter sort mean_rd 	if variables == "age18rd_ahinc" & gp == "`ue2'", color(red*1.5) msize(small) msymbol(X))
		
		(scatter sort mean_rd  if variables == "age18rd_kidemp15" & gp == "all", color(green) msize(small) msymbol(o))
		(scatter sort mean_rd  if variables == "age18rd_kidemp15" & gp == "`fam1'", color(green) msize(small) msymbol(o))
		(scatter sort mean_rd  if variables == "age18rd_kidemp15" & gp == "`fam2'", color(green) msize(small) msymbol(o))
		(scatter sort mean_rd 	if variables == "age18rd_kidemp15" & gp == "`earn1'", color(green) msize(small) msymbol(o))
		(scatter sort mean_rd 	if variables == "age18rd_kidemp15" & gp == "`earn2'", color(green) msize(small) msymbol(o))
		(scatter sort mean_rd 	if variables == "age18rd_kidemp15" & gp == "`sex1'", color(green) msize(small) msymbol(o))
		(scatter sort mean_rd	if variables == "age18rd_kidemp15" & gp == "`sex2'", color(green) msize(small) msymbol(o))
		(scatter sort mean_rd 	if variables == "age18rd_kidemp15" & gp == "`diag2'", color(green) msize(small) msymbol(o))
		(scatter sort mean_rd 	if variables == "age18rd_kidemp15" & gp == "`diag3'", color(green) msize(small) msymbol(o))
		(scatter sort mean_rd 	if variables == "age18rd_kidemp15" & gp == "`diag4'", color(green) msize(small) msymbol(o))
		(scatter sort mean_rd 	if variables == "age18rd_kidemp15" & gp == "`race1'", color(green) msize(small) msymbol(o))
		(scatter sort mean_rd 	if variables == "age18rd_kidemp15" & gp == "`race2'", color(green) msize(small) msymbol(o))
		(scatter sort mean_rd 	if variables == "age18rd_kidemp15" & gp == "`ue1'", color(green) msize(small) msymbol(o))
		(scatter sort mean_rd 	if variables == "age18rd_kidemp15" & gp == "`ue2'", color(green) msize(small) msymbol(o))
		
		(scatter sort mean_rd if var == "kidem", lcolor(none) lwidth(medthick) yaxis(2))
		
		, 
		legend(off)
		xsc(r(0 .45)) xtitle("Increase in incar / emp ($15K) / income-gen charge", size(medsmall))
		xlab(0(.1).4)
		ysc(r(2 25) axis(1)) 
		ysc(r(2 25) axis(2)) 
		ytitle("", axis(1)) 
		ytitle("", axis(2))
		ylabel(
		   24 "All" 22 "Two parents"  20 "Single/no parent" 18 "High parent earn" 16 "Low parent earn" 
			14 "White" 12 "Black" 10 "Female" 8 "Male" 6 "Intellectual" 4 "Physical" 2 "Mental" 0 "Low unemp cnty" -2 "High unemp cnty", 
		   angle(0) notick labsize(small) /*nogrid*/ axis(1) ) 
		ylabel(
		   24 "All" 22 "Two parents"  20 "Single/no parent" 18 "High parent earn" 16 "Low parent earn" 
			14 "White" 12 "Black" 10 "Female" 8 "Male" 6 "Intellectual" 4 "Physical" 2 "Mental" 0 "Low unemp cnty" -2 "High unemp cnty", 
		   angle(0) notick labsize(small) /*nogrid*/ axis(2) ) 
		graphregion(color(white))
		bgcolor(white)
		ylabel(,nogrid angle(0) axis(1))
		ylabel(,nogrid angle(0) axis(2))
		ttext(25 .05 "Incarceration", 		size(small) color(red*1.5))
		ttext(25 .15  "Employment", 		size(small) color(green))
		ttext(25 .3  "Income-gen charge", 	size(small) color(orange))
		plotregion(margin(0 2 2 2))  
		;
		graph export "`output'/fig6.png", width(1000) height(750) replace ;
